package com.xq.pethome.system.logininfor.controller;

import com.xq.pethome.system.logininfor.service.ILogininfoService;
import com.xq.pethome.system.logininfor.domain.Logininfo;
import com.xq.pethome.system.logininfor.query.LogininfoQuery;
import com.xq.pethome.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/logininfo")
public class LogininfoController {
    @Autowired
    public ILogininfoService logininfoService;


    /**
     * 保存和修改公用的
     * @param logininfo  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Logininfo logininfo){
        try {
            if( logininfo.getId()!=null)
                logininfoService.update(logininfo);
            else
                logininfoService.add(logininfo);
            return AjaxResult.success().setMsg("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            logininfoService.delete(id);
            return AjaxResult.success().setMsg("保存成功");
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public Logininfo get(@PathVariable("id")Long id)
    {
        return logininfoService.findById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Logininfo> list(){

        return logininfoService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public AjaxResult json(@RequestBody LogininfoQuery query)
    {
        try {
            List<Logininfo> departments = logininfoService.findQuery(query);
            Long count = logininfoService.findCount(query);
            return AjaxResult.success().setData(departments).setTotal(count);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }
}
